package com.xbongbong.paas.connection.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.connection.pojo.dto.CloudCodeLowCodeConfigListDTO;
import com.xbongbong.paas.connection.pojo.dto.LowCodeInstallDTO;
import com.xbongbong.paas.connection.pojo.vo.CloudCodeLowCodeIsDeveloperVO;
import com.xbongbong.paas.connection.pojo.vo.CompanyInfoVO;
import com.xbongbong.paas.connection.service.ICloudCodeService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.developer.pojo.dto.DeveloperListDTO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.lowcodeconfig.pojo.vo.LowCodeConfigListVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 云叩与pro数据交互接口
 * @author 李少杰
 * @version v1.0.0
 * @date 2022/2/10 17:23
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/connection/cloudCode")
public class CloudCodeController {

    @Resource
    private ICloudCodeService iCloudCodeService;

    /**
     * 获取表单配置
     * @param cloudCodeLowCodeConfigListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/formConfigList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid CloudCodeLowCodeConfigListDTO cloudCodeLowCodeConfigListDTO, BindingResult br) throws Exception {
        XbbResponse<LowCodeConfigListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LowCodeConfigListVO lowCodeConfigListVO = iCloudCodeService.lowCodeConfigList(cloudCodeLowCodeConfigListDTO);
                response = new XbbResponse<>(lowCodeConfigListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取公司信息
     * @param jsonObject
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/company/info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String companyInfo(@RequestBody @Valid JSONObject jsonObject, BindingResult br) throws Exception {
        XbbResponse<CompanyInfoVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                String corpid = jsonObject.getString("corpid");
                CompanyInfoVO companyInfoVO = iCloudCodeService.companyInfo(corpid);
                response = new XbbResponse<>(companyInfoVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 低代码安装
     * @param lowCodeInstallDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/lowCode/install", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String lowCodeInstall(@RequestBody @Valid LowCodeInstallDTO lowCodeInstallDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = iCloudCodeService.lowCodeInstall(lowCodeInstallDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 低代码回退
     * @param cloudCodeLowCodeConfigListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/lowCode/back", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String lowCodeBack(@RequestBody @Valid CloudCodeLowCodeConfigListDTO cloudCodeLowCodeConfigListDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = iCloudCodeService.backLowCode(cloudCodeLowCodeConfigListDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 校验用户是否为云叩开发者
     * @param developerListDTO
     * @param br
     * @return
     * @throws Exception
     * @date: 2022/6/28
     */
    @RequestMapping(value = "/isDeveloper", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String isDeveloper(@RequestBody @Valid DeveloperListDTO developerListDTO, BindingResult br) throws Exception{
        XbbResponse<CloudCodeLowCodeIsDeveloperVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CloudCodeLowCodeIsDeveloperVO isDeveloperVO = iCloudCodeService.isDeveloper(developerListDTO);
                response = new XbbResponse<>(isDeveloperVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
